
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;

public class GridView extends Canvas {

    private String[] label = {"Rank", "Genres", "Singer", "Search", "My Music", "Info"};
    private int selectedIndex;
    private Image[] iconArray;
    private Image bgImage, logoImg;
    private int top, lrb, cols, rows, width, height, text;
    public Command CMD_EXIT = new Command("Exit",Command.EXIT,0);

    public GridView() {
        top = 120;
        width = 64;
        height = 84;
        cols = getWidth()/width;
        rows = (getHeight()-top)/height;
        lrb = (getWidth()-cols*width)/2;
        iconArray = new Image[6];
        this.addCommand(CMD_EXIT);
        try {
            iconArray[0] = Image.createImage("/rank.png");
            iconArray[1] = Image.createImage("/genres.png");
            iconArray[2] = Image.createImage("/singer.png");
            iconArray[3] = Image.createImage("/search.png");
            iconArray[4] = Image.createImage("/mymusic.png");
            iconArray[5] = Image.createImage("/info.png");
            bgImage = Image.createImage("/background.jpg");
            logoImg = Image.createImage("/logo.png");
        } catch (Exception ex) {}
    }

    protected void paint(Graphics g) {
        g.drawImage(bgImage, 0, 0, Graphics.TOP | Graphics.LEFT);
        g.drawImage(logoImg, getWidth()/2, top/2, Graphics.VCENTER | Graphics.HCENTER);
        g.setColor(0xff0000);
        g.fillRect(lrb + (selectedIndex % 3) * width, top + (selectedIndex / 3) * height, width, width);
        Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
        g.setFont(f);
        for (int i = 0; i < label.length; i++) {
            g.drawRegion(iconArray[i], 0, 0, 48, 48, Sprite.TRANS_NONE, lrb + (i % cols) * width + width / 2, top + (i / 3) * height + width / 2, Graphics.VCENTER | Graphics.HCENTER);
            g.drawString(label[i], lrb + (i % 3) * width + width / 2, top + (i / 3) * height + width, Graphics.TOP | Graphics.HCENTER);

        }
    }

    protected void keyPressed(int keyCode) {
        if (getKeyName(keyCode).equals("Select")) {
            onSelect(selectedIndex);
        }
        if (this.getKeyName(keyCode).equals("Right")) {
            selectedIndex = (byte) (++selectedIndex % label.length);
            repaint();
        }
        if (getKeyName(keyCode).equals("Left")) {
            selectedIndex = (--selectedIndex) % label.length;
            if (selectedIndex == -1) {
                selectedIndex = label.length - 1;
            }
            repaint();
        }
        if (getKeyName(keyCode).equals("Up")) {
            selectedIndex -= cols;
            if (selectedIndex < 0) {
                selectedIndex = ((label.length - 1) / 3) * cols + (selectedIndex + cols);
            }
            if (selectedIndex >= label.length) {
                selectedIndex = label.length - 1;
            }
            repaint();
        }
        if (getKeyName(keyCode).equals("Down")) {
            selectedIndex += cols;
            if (selectedIndex >= label.length) {
                if (selectedIndex / 3 == (label.length - 1) / 3) {
                    selectedIndex = label.length - 1;
                } else {
                    selectedIndex = selectedIndex % 3;
                }
            }
            repaint();
        }
    }

    public void onSelect(int index) {
        if (index == 0) {
            f_readsong frm1 = new f_readsong(0);
            frm1.load("rank.php?type=song");
            f_readplaylist frm2 = new f_readplaylist(0);
            frm2.load("rank.php?type=playlist");
            frm1.title = frm2.title = "Rank";
            AppManager.open(frm1, frm2, true);
        } else if (index == 1) {
            Categories frm = new Categories();
            frm.load("category.php?type=genres", 0);
            AppManager.open(frm);
            //AppManager.example.load("category.php?type=singer", 1);
        } else if (index == 2) {
            Categories frm = new Categories();
            frm.load("rank.php?type=singer", 1);
            AppManager.open(frm);
        } else if (index == 3) {
            AppManager.open(new Search());
        } else if (index == 4) {
            AppManager.open(new Login());
        } else if (index == 5) {
            AppManager.open(new f_info());
        }
    }
}
